package cn.tedu._05mvcboot01.controller;

import cn.tedu._05mvcboot01.pojo.dto.BodyInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class BMIController {
    /**
     * 方式1:声明变量
     * 请求地址:/bmi
     */
//    @RequestMapping("/bmi")
//    @ResponseBody
//    public String bmiTest(Double height, Double weight) {
//        double bmi = (int) (weight / (height * height) * 10) / 10.0;
//        if (bmi < 18.5)
//            return "bmi=" + bmi + ",偏瘦";
//        if (bmi < 24)
//            return "bmi=" + bmi + ",正常";
//        if (bmi < 27)
//            return "bmi=" + bmi + ",偏胖";
//        return "bmi=" + bmi + ",该减肥了";
//    }

    /**
     * 方式2:pojo类接收
     * 请求地址:/bmi
     */
    @RequestMapping("/bmi")
    @ResponseBody
    public String bmiTest(BodyInfo bodyInfo) {
        Double height = bodyInfo.getHeight();
        Double weight = bodyInfo.getWeight();
        double bmi = (int) (weight / (height * height) * 10) / 10.0;
        if (bmi < 18.5)
            return "bmi=" + bmi + ",偏瘦";
        if (bmi < 24)
            return "bmi=" + bmi + ",正常";
        if (bmi < 27)
            return "bmi=" + bmi + ",偏胖";
        return "bmi=" + bmi + ",该减肥了";
    }
}
